#include "SceneManager.h"
#include <QtGlobal>
#include <osg/Group>

SceneManager::SceneManager()
{
	createGroups();
}


void SceneManager::addObject(SceneManager::ObjectType type, osg::Node *pNode)
{
	osg::Group* pGroup = getObjectGroup(type);
	Q_ASSERT(pGroup);
	pGroup->addChild(pNode);
}


void SceneManager::removeObject(SceneManager::ObjectType type, osg::Node *pNode)
{
	osg::Group* pGroup = getObjectGroup(type);
	Q_ASSERT(pGroup);
	pGroup->removeChild(pNode);
}


const osg::Group* SceneManager::getObjectGroup(SceneManager::ObjectType type) const
{
	TypeGroupMap::const_iterator it = m_TypeGroupMap.find(type);
	if(it == m_TypeGroupMap.end())
	{
		return NULL;
	}
	return it->second.get();
}


osg::Group* SceneManager::getObjectGroup(SceneManager::ObjectType type)
{
	TypeGroupMap::iterator it = m_TypeGroupMap.find(type);
	if(it == m_TypeGroupMap.end())
	{
		return NULL;
	}
	return it->second.get();
}


void SceneManager::createGroups()
{
	m_rRoot = new osg::Group;
	m_rRoot->setName("Root");

	{
		osg::Group* pGroup = new osg::Group;
		pGroup->setName("Models");
		pGroup->setDataVariance(osg::Object::DYNAMIC);
		m_rRoot->addChild(pGroup);
		m_TypeGroupMap.insert(TypeGroupMap::value_type(Model, pGroup));
	}

	{
		osg::Group* pGroup = new osg::Group;
		pGroup->setName("Internals");
		pGroup->setDataVariance(osg::Object::DYNAMIC);
		m_rRoot->addChild(pGroup);
		m_TypeGroupMap.insert(TypeGroupMap::value_type(Internal, pGroup));
	}
}
